package com.school.aftersalemanagement.controller.ui.scancode;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.school.aftersalemanagement.R;
import com.school.aftersalemanagement.controller.ui.bicyclenumopen.BicycleNumberOpenActivity;
import com.school.aftersalemanagement.global.BaseToolBarActivity;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import butterknife.OnClick;

/**
 * Created by winkyle on 2017/10/23.
 */

public class ScanCodeActivity extends BaseToolBarActivity {
    private CaptureFragment captureFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scancode);
        setToolbarText("扫码开锁");
        captureFragment = new CaptureFragment();
        // 为二维码扫描界面设置定制化界面
        CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);
        captureFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(R.id.scancode_camera, captureFragment).commit();
    }

    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            result = result.substring(result.indexOf("#") + 1);
            bundle.putString("bikeNum", result);
            resultIntent.putExtras(bundle);
            ScanCodeActivity.this.setResult(RESULT_OK, resultIntent);
            ScanCodeActivity.this.finish();
        }

        @Override
        public void onAnalyzeFailed() {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putString("bikeNum", "");
            resultIntent.putExtras(bundle);
            ScanCodeActivity.this.setResult(RESULT_OK, resultIntent);
            ScanCodeActivity.this.finish();
        }
    };

    public static boolean isOpen = false;
    @OnClick({R.id.scancode_open, R.id.scancode_inputNumber})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.scancode_inputNumber:
                Intent intent = new Intent(this,BicycleNumberOpenActivity.class);
                startActivityForResult(intent, 200);
                break;
            case R.id.scancode_open:
                if (!isOpen) {
                    CodeUtils.isLightEnable(true);
                    isOpen = true;
                } else {
                    CodeUtils.isLightEnable(false);
                    isOpen = false;
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 200) {
            //处理扫描结果（在界面上显示）
            if (null != data&&data.getExtras()!=null) {
                Bundle bundle = data.getExtras();
                Intent resultIntent = new Intent();
                resultIntent.putExtras(bundle);
                ScanCodeActivity.this.setResult(RESULT_OK, resultIntent);
                ScanCodeActivity.this.finish();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
